package de.revdev.llt.bean.facade;

import de.revdev.llt.bean.entities.FileCardBox;
import de.revdev.llt.bean.entities.PrivateClassRoom;
import de.revdev.llt.bean.entities.PrivateClassRoom_;
import de.revdev.llt.bean.entities.PublicClassRoom;
import de.revdev.llt.bean.entities.PublicClassRoom_;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

/**
 *
 * @author f.lindermaier
 */
@Stateless
public class PrivateClassRoomFacade extends AbstractFacade<PrivateClassRoom> {
    @PersistenceContext(unitName = "LetsLearnTogetherPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public PrivateClassRoomFacade() {
        super(PrivateClassRoom.class);
    }
    
        public PrivateClassRoom getFCBforPuCR(PrivateClassRoom privateClassRoom) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<PrivateClassRoom> cq = cb.createQuery(PrivateClassRoom.class);
            Root<PrivateClassRoom> r = cq.from(PrivateClassRoom.class);
            Fetch<PrivateClassRoom, FileCardBox> f = r.fetch(PrivateClassRoom_.fileCardBoxes);
            cq.select(r);
            ParameterExpression<Long> p = cb.parameter(Long.class);
            cq.where(cb.equal(r.get(PrivateClassRoom_.id), p));
            return em.createQuery(cq).setParameter(p, privateClassRoom.getId()).getSingleResult();
        } catch (Exception e) {
            return privateClassRoom;
        }
    }
}
